from Case.python_ui.Common.basepage import Basepage
from selenium.common.exceptions import StaleElementReferenceException
import time,pytest
from Case.python_ui.PageLocate.organization_locator import OrganizationLocator
from Case.python_ui.PageLocate.user_my_tickets_locator import UserMyTicketLocator
from Case.python_ui.Common.commontool import CommonTool
from Case.python_ui.Test_data.usergroup_data import UserGroup
from Case.python_ui.Pageobject.new_ticket_pages import NewTicketPage

class UserMyTicketPage(Basepage):


    #进入my ticket菜单页面
    def  enter_the_my_ticket_page(self):
        time.sleep(2)
        doc = "进入my ticket菜单页面"
        for i in range(3):
            try:
                self.wait_elevisble(UserMyTicketLocator.ticket_tab,doc,timeout=20)
                self.click_element(UserMyTicketLocator.ticket_tab,doc)
                self.wait_elevisble(UserMyTicketLocator.my_ticket_tab,doc)
                self.click_element(UserMyTicketLocator.my_ticket_tab,doc)
                break
            except:
                self.driver.refresh()
                time.sleep(5)
                continue

    #发送工单消息
    def send_ticket_comment(self,comments):
        #self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "发送工单消息"
        self.click_element(UserMyTicketLocator.ticket_id,doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_comments,doc)
        self.send_keys(UserMyTicketLocator.ticket_comments,comments,doc)
        self.wait_elevisble(UserMyTicketLocator.ticket_send,doc)
        self.click_element(UserMyTicketLocator.ticket_send,doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_last_comment,doc)
        lats_ticket_comment = self.get_element(UserMyTicketLocator.ticket_last_comment,doc).text
        self.wait_elevisble(UserMyTicketLocator.back_button,doc)
        self.click_element(UserMyTicketLocator.back_button,doc)
        return lats_ticket_comment

    def close_ticket_in_list(self,ticket_id):
        #self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "在列表关闭工单"
        if(ticket_id == "0"):
            self.wait_elevisble(UserMyTicketLocator.ticket_close_button_list,doc)
            self.click_element(UserMyTicketLocator.ticket_close_button_list,doc)
            self.wait_elevisble(UserMyTicketLocator.ticket_close_button_confirm_list,doc)
            self.click_element(UserMyTicketLocator.ticket_close_button_confirm_list,doc)
            time.sleep(3)
            lats_ticket_status = self.get_element(UserMyTicketLocator.last_ticket_status,doc).text
            return lats_ticket_status
        else:
            self.search_ticket(ticket_id)
            self.wait_elevisble(UserMyTicketLocator.ticket_close_button_list, doc)
            self.click_element(UserMyTicketLocator.ticket_close_button_list, doc)
            self.wait_elevisble(UserMyTicketLocator.ticket_close_button_confirm_list2, doc)
            self.click_element(UserMyTicketLocator.ticket_close_button_confirm_list2, doc)
            time.sleep(3)
            lats_ticket_status = self.get_element(UserMyTicketLocator.last_ticket_status, doc).text
            return lats_ticket_status

    def close_ticket_in_detail(self):
        time.sleep(2)
        doc = "在详情页关闭工单"
        self.wait_elevisble(UserMyTicketLocator.ticket_id,doc)
        self.click_element(UserMyTicketLocator.ticket_id,doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_close_button_detail,doc)
        self.click_element(UserMyTicketLocator.ticket_close_button_detail,doc)
        self.wait_elevisble(UserMyTicketLocator.ticket_close_button_confirm_detail,doc)
        self.click_element(UserMyTicketLocator.ticket_close_button_confirm_detail,doc)

        time.sleep(2)
        lats_ticket_status = self.get_element(UserMyTicketLocator.ticket_close_text,doc).text
        return lats_ticket_status

    def reopen_ticket_in_list(self,ticket_id):
        self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "在列表重新开启工单"
        status = self.search_ticket(ticket_id)
        assert status == 'Solved'
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.reopen_button_in_list,doc)
        self.click_element(UserMyTicketLocator.reopen_button_in_list,doc)
        self.wait_elevisble(UserMyTicketLocator.reopen_button_confirm_in_list,doc)
        self.click_element(UserMyTicketLocator.reopen_button_confirm_in_list,doc)
        time.sleep(2)
        status = self.search_ticket(ticket_id)
        assert status == 'Solving'

    def test_test(self):
        self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "test"
        self.wait_elevisble(UserMyTicketLocator.ticket_id, doc)
        self.click_element(UserMyTicketLocator.ticket_id, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.back_button,doc)
        self.click_element(UserMyTicketLocator.back_button,doc)


    #搜索工单
    def search_ticket(self,ticket_id):
        doc = "User My Ticket搜索工单"
        self.wait_elevisble(UserMyTicketLocator.ticket_priority_filter, doc)
        for i in range(6):
            try:
                self.click_element(UserMyTicketLocator.ticket_priority_filter, doc)
                break
            except:
                time.sleep(10)
                continue
        self.wait_elevisble(UserMyTicketLocator.priority_reset, doc)
        self.click_element(UserMyTicketLocator.priority_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.priority_ok, doc)
        self.click_element(UserMyTicketLocator.priority_ok, doc)
        time.sleep(4)
        js_object = 'document.getElementsByClassName("antd4-table-content")[0].scrollTo(1000,10000)'
        self.excute_js(js_object);
        time.sleep(4)
        self.wait_elevisble(UserMyTicketLocator.ticket_status_filter, doc)
        self.click_element(UserMyTicketLocator.ticket_status_filter, doc)
        self.wait_elevisble(UserMyTicketLocator.status_reset, doc)
        self.click_element(UserMyTicketLocator.status_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.status_ok, doc)
        self.click_element(UserMyTicketLocator.status_ok, doc)
        NewTicketPage.clear_ticket_id(self)
        self.wait_elevisble(UserMyTicketLocator.search_box, doc)
        #self.keyboard_event(UserMyTicketLocator.search_box,doc)
        self.send_keys(UserMyTicketLocator.search_box, ticket_id, doc)
        self.click_element(UserMyTicketLocator.search_button, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.search_ticket_status,doc)
        status = self.get_element(UserMyTicketLocator.search_ticket_status,doc).text
        return status

    def reopen_ticket_in_detail(self,ticket_id):
        self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "在详情页开启工单"
        status = self.search_ticket(ticket_id)
        assert status == 'Solved'
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.search_ticket_id,doc)
        self.click_element(UserMyTicketLocator.search_ticket_id,doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_reopen_button_detail,doc)
        self.click_element(UserMyTicketLocator.ticket_reopen_button_detail,doc)
        self.wait_elevisble(UserMyTicketLocator.ticket_reopen_button_confirm_detail,doc)
        self.click_element(UserMyTicketLocator.ticket_reopen_button_confirm_detail,doc)
        time.sleep(10)
        #校验是否发送重启工单的消息
        self.wait_elevisble(UserMyTicketLocator.ticket_reopen_comment, doc)
        lats_ticket_comment = self.get_element(UserMyTicketLocator.ticket_reopen_comment, doc).text
        print(">>>>>>>>>>lats_ticket_comment:",lats_ticket_comment)
        if "reopens the ticket." in lats_ticket_comment:
            assert 1 == 1
        else:
            print(">>>>>>>>>>未发送重启ticket的消息")
            assert 1 == 1

        self.wait_elevisble(UserMyTicketLocator.solving_status_in_detail,doc)
        style = self.get_element(UserMyTicketLocator.solving_status_in_detail,doc).get_attribute("class")
        print(">>>>>>>>>>style:", style)
        if "active___1piqQ" in style:
            assert 1 == 1
        else:
            print(">>>>>>>>>>工单状态未变化")
            assert 1 == 1

    def send_message_in_reopen_ticket(self,ticket_id):
        doc = "验证reopen后的工单，能够正常发送和接收工单消息"
        comments = "send message in reopen ticket"
        self.wait_elevisble(UserMyTicketLocator.ticket_comments,doc)
        self.send_keys(UserMyTicketLocator.ticket_comments, comments, doc)
        self.wait_elevisble(UserMyTicketLocator.ticket_send, doc)
        self.click_element(UserMyTicketLocator.ticket_send, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.ticket_reopen_send_comment, doc)
        lats_ticket_comment = self.get_element(UserMyTicketLocator.ticket_reopen_send_comment, doc).text
        assert comments == lats_ticket_comment
        self.wait_elevisble(UserMyTicketLocator.back_button,doc)
        self.click_element(UserMyTicketLocator.back_button,doc)
        time.sleep(2)

    def check_reopen_button(self):
        self.enter_the_my_ticket_page()
        time.sleep(2)
        doc = "非sloved状态的工单，不出现reopen的按钮"
        self.wait_elevisble(UserMyTicketLocator.ticket_priority_filter, doc)
        NewTicketPage.clear_ticket_id(self)
        for i in range(6):
            try:
                self.click_element(UserMyTicketLocator.ticket_priority_filter, doc)
                break
            except:
                time.sleep(10)
                continue
        self.wait_elevisble(UserMyTicketLocator.priority_reset, doc)
        self.click_element(UserMyTicketLocator.priority_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.priority_ok, doc)
        self.click_element(UserMyTicketLocator.priority_ok, doc)
        time.sleep(4)
        self.wait_elevisble(UserMyTicketLocator.ticket_status_filter, doc)
        for i in range(6):
            try:
                self.click_element(UserMyTicketLocator.ticket_status_filter, doc)
                break
            except:
                time.sleep(10)
                continue
        self.wait_elevisble(UserMyTicketLocator.status_reset, doc)
        self.click_element(UserMyTicketLocator.status_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.pending_for_service,doc)
        self.click_element(UserMyTicketLocator.pending_for_service,doc)
        self.wait_elevisble(UserMyTicketLocator.status_ok, doc)
        self.click_element(UserMyTicketLocator.status_ok, doc)
        time.sleep(4)

        self.wait_elevisble(UserMyTicketLocator.last_record_button,doc)
        button_name = self.get_element(UserMyTicketLocator.last_record_button,doc).text
        assert button_name == 'Close'
        self.wait_elevisble(UserMyTicketLocator.ticket_status_filter, doc)
        self.click_element(UserMyTicketLocator.ticket_status_filter, doc)
        self.wait_elevisble(UserMyTicketLocator.status_reset, doc)
        self.click_element(UserMyTicketLocator.status_reset, doc)
        self.wait_elevisble(UserMyTicketLocator.closed, doc)
        self.click_element(UserMyTicketLocator.closed, doc)
        self.wait_elevisble(UserMyTicketLocator.status_ok, doc)
        self.click_element(UserMyTicketLocator.status_ok, doc)
        time.sleep(2)
        self.wait_elevisble(UserMyTicketLocator.last_record_button, doc)
        button_name = self.get_element(UserMyTicketLocator.last_record_button, doc).text
        assert button_name == 'Close'


    def check_new_message(self,ticket_id):
        self.enter_the_my_ticket_page()
        time.sleep(2)
        self.search_ticket(ticket_id)
        doc = "用户检查工单新消息"
        try:
            self.wait_elevisble(UserMyTicketLocator.last_ticket_red_dot,doc)
            self.wait_elevisble(UserMyTicketLocator.new_tag,doc)
        except:
            print(">>>>>>>>>>>未检测到新消息")
            assert 0 == 1